package com.example.trueorfalse.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.trueorfalse.R;
import com.example.trueorfalse.model.Question;
import com.example.trueorfalse.tools.QuestionLab;


public class ListQuestionActivity extends AppCompatActivity {
    private static final String EXTRA_TAG = "com.example.trueorfalse.question";

    private RecyclerView mQuestionRecyclerView;
    private Button mSendButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_list);
        mQuestionRecyclerView = findViewById(R.id.list_question_recyclerview);
        mQuestionRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        QuestionAdapter question = new QuestionAdapter(QuestionLab.get().getQuestions());
        mQuestionRecyclerView.setAdapter(question);
        mSendButton = findViewById(R.id.send_button);
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QuestionLab.get().sendMessage(ListQuestionActivity.this);
            }
        });

    }

    private class QuestionAdapter extends RecyclerView.Adapter<QuestionHolder>{
        private Question[] mQuestions;

        public QuestionAdapter(Question[] questions) {
            mQuestions = questions;
        }

        @NonNull
        @Override
        public QuestionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.question_item, parent, false);
            QuestionHolder holder = new QuestionHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull QuestionHolder holder, int position) {
            Question question = mQuestions[position];
            holder.bind(question,position+1);
        }

        @Override
        public int getItemCount() {
            return mQuestions.length;
        }
    }

    private class QuestionHolder extends RecyclerView.ViewHolder{
        private TextView mQuestionTextView;
        private TextView mAnswerTextView;
        private TextView mNumberTextView;

        public QuestionHolder(@NonNull View itemView) {
            super(itemView);
            mQuestionTextView = itemView.findViewById(R.id.question);
            mAnswerTextView = itemView.findViewById(R.id.answer);
            mNumberTextView = itemView.findViewById(R.id.num);
        }

        public void bind(Question question,int postion){
            mQuestionTextView.setText(question.getTextResId());
            mAnswerTextView.setText(question.getAnswer());
            mNumberTextView.setText(postion+"、");
        }

    }


    public static Intent newIntent(Context context, String data){
        Intent intent = new Intent(context, ListQuestionActivity.class);
        intent.putExtra(EXTRA_TAG,data);
        return intent;
    }


}